$(function () {

    $(document).ready(function () {


        //init codemirror
        var editor = CodeMirror.fromTextArea(document.getElementById("sql-textarea"), {
            mode: "text/x-mysql",
            tabMode: "indent",
            lineNumbers: true,
        });


        $("#schema-explorer-button").click(function () {
             window.open("schema-explorer");
            return false;
        });


        //submit query
        $("#submit-button").click(function () {
            var post = {};
            post.sql = editor.getValue();
            var schema_id = $("#query-schemata-select option:selected").val();

            //disable submit
            $("#submit-button").attr("disabled", '');

            var check = null;
            //start animated progres bar
            if (check == null) {
                var cnt = 0;

                check = setInterval(function () {

                if(cnt == 100)
                    cnt = 0;

                $("#progress").css('width',cnt+'%');
                cnt += 4;
                  
                }, 400);
            }

            //send POST to API
            $.ajax({
                url: 'query/submit/' + schema_id,
                dataType: "json",
                data: post,
                type: "POST",
                success: function (response) {

                    //check for success
                    if (response.success) {
                        //empty table
                        $("#table-body-query-result").empty();
                        //empty header
                        $("#table-head-query-result").empty();

                        //show results
                        if (response.data != null && response.data.length > 0) {

                            var row = "";
                            //build header based on first row
                            for (var k in response.data[0]) {
                                row += "<th>";
                                row += k;
                                row += "</th>";
                            }
                           
                            //add header
                            $("#table-head-query-result").append(row);                            

                            //iterate data rows
                            $.each(response.data, function (key, value) {

                                var row = "<tr>";
                                for (var k in value) {
                                    row += "<td>";
                                    row += value[k];
                                    row += "</td>";
                                }
                                row += "</tr>";
                            
                                //add row to table
                                $("#table-body-query-result").last().append(row);
                            });

                        }else{
                            //warn no results
                        }

                        //enable
                        $("#submit-button").removeAttr("disabled"); 
                        $("#div-query-result").removeClass("hide");
                       
                    }else{
                        //Alert

                    }

                    //stop animated bar
                    clearInterval(check);
                    check = null;
                }
            });
        });
    });
});
